package personal.xjl._23java1studentdemo.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import personal.xjl._23java1studentdemo.model.Student;
import personal.xjl._23java1studentdemo.model.Teacher;
import personal.xjl._23java1studentdemo.service.AdminService;
import personal.xjl._23java1studentdemo.service.StudentService;
import personal.xjl._23java1studentdemo.service.TeacherService;

import java.util.List;

@Controller
public class LoginController {
    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    AdminService adminService;

    @RequestMapping("login")
    public String login(){
        return "login";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();//将session取消掉，让令牌无效
        return "login";
    }

    @RequestMapping("loginValidate")
    public String login(int role,String username, String password, Model model, HttpSession session){
        switch (role){
            case 1:{
                if ( studentService.login(username,password)){
                    session.setAttribute("username", username);
                    return "redirect:/Student/list/1/3";
                }else{
                    return "redirect:/login";
                }
            }
            case 2:{
                if ( teacherService.login(username,password)){
                    session.setAttribute("username", username);
                    return "redirect:/Teacher/list/1/3";
                }else{
                    return "redirect:/login";
                }
            }
            case 3:{
                if ( adminService.login(username,password)){
                    session.setAttribute("username", username);
                    return "redirect:/Course/list/1/3";
                }else{
                    return "redirect:/login";
                }
            }
            default:{
                return "redirect:/login";
            }
        }

    }

}
